<?php

namespace App\Http\Controllers;

use App\Http\Logic\MachineLogic;
use App\Http\Requests\MachineRequest;
use App\Logic\GoodsLogic;
use App\Services\DaHuangService;
use Illuminate\Support\Facades\Storage;

class MachineController extends BaseController
{
    /**
     * @notes 获取设备内商品
     * @param MachineRequest $request
     * @return \Dingo\Api\Http\Response
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/06/14 15:06
     */
    public function getMachineGoods(MachineRequest $request)
    {
        $machine_code = $request -> input('machineCode');

        $machine_info = MachineLogic::getMachineInfo($machine_code);

        if($machine_info === false) {
            return $this->error(MachineLogic::getErrorMessage());
        }

        $goods_list = [];

        switch($machine_info['source_type'])
        {
            case 1 :
                $res = DaHuangService::getDoorGoods($machine_info['machine_detail']['device_code']);
                if($res && is_array($res)) {
                    foreach($res as $v) {
                        $goods_list[] = [
                            'name'   => $v['skuName'],
                            'picture'=> $v['skuPicUrl'],
                            'price'  => bcdiv($v['skuSellingPrice'], 100, 2)
                        ];
                    }
                }
            break;
            case 2:
                $res = GoodsLogic::getDoorGoods($machine_info['id']);
                if($res) {
                    foreach($res as $v) {
                        $goods_list[] = [
                            'name'   => $v['goods']['goods_name'],
                            'picture'=> Storage::url($v['goods']['goods_picture']),
                            'price'  => bcdiv($v['sell_price'], 100, 2)
                        ];
                    }
                }
            break;
        }

        return $this->success(['goodsList' => $goods_list]);
    }
}
